//
//  EditorViewController.m
//  Article
//
//  Created by LONG on 16/4/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "EditorViewController.h"
#import "DetailTitleView.h"
#import "AddViewController.h"//添加
#import "RecommendViewController.h"//推荐
#import "HotViewController.h"//热文
#import "MyViewController.h"//我的
#import "LiZhiViewController.h"//励志
#import "CarViewController.h"//汽车
#import "KeJiViewController.h"//科技
#import "ShiShangViewController.h"//时尚
#import "CaiJingViewController.h"//财经
#import "YuLeViewController.h"//娱乐

@interface EditorViewController ()<DetailTitleViewDelegate,UIScrollViewDelegate>
//头部的选项卡
@property(nonatomic,strong) DetailTitleView *titleView;

//滚动条
@property(nonatomic,strong) UIScrollView *scrollView;

//大数组，子控制器的
@property(nonatomic,strong) NSMutableArray *childViews;

@property (nonatomic,strong)NSMutableArray *dataarr;
@end

@implementation EditorViewController
- (NSMutableArray *)childViews
{
    if(_childViews==nil)
    {
        _childViews =[NSMutableArray array];
    }
    return _childViews;
}

- (UIScrollView *)scrollView
{
    if(_scrollView==nil)
    {
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - _titleView.frame.size.height)];
        _scrollView.delegate=self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.contentSize = CGSizeMake(2 * WIDTH, 0);
        _scrollView.bounces = NO;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}


- (DetailTitleView *)titleView
{
    if(_titleView==nil)
    {
        _titleView =[DetailTitleView new];
        _titleView.layer.borderColor = [UIColor whiteColor].CGColor;
        _titleView.layer.masksToBounds = YES;
        _titleView.layer.cornerRadius = 6.0;
        _titleView.layer.borderWidth = 1.0;
        _titleView.layer.borderColor = [UIColorFromRGB(0xffffff) CGColor];
        _titleView.delegate=self;
        _titleView.frame = CGRectMake(0, 0, 150, 25);
        self.navigationItem.titleView = _titleView;
        
    }
    return _titleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //nav底部线消失
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationController.navigationBar.translucent = NO;

    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    //添加头部的view
    [self titleView];
    
    //添加scrollView
    [self scrollView];
    
    

    [self requestTagslist];
}

- (void)requestTagslist{
    if (_dataarr == nil) {
    _dataarr = [[NSMutableArray alloc]init];
    
    }
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:TAGSLIST_URL parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                [_dataarr addObject:[dic objectForKey:@"tags"]];
            }
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        //添加各子控制器
        [self setupChildVcs];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark 添加各子控制器
- (void)setupChildVcs
{
    //推荐控制器
    RecommendViewController *foodVc =[RecommendViewController new];
    
    [self.childViews addObject:foodVc.view];
    
    //添加控制器
    AddViewController *evaluateVc =[AddViewController new];
    
    [self addChildViewController:evaluateVc];
    [self.childViews addObject:evaluateVc.view];
    
    
    
    for(int i=0;i<self.childViews.count;i++)
    {
        UIView *childV = self.childViews[i];
        CGFloat childVX = WIDTH * i ;
        childV.frame = CGRectMake(childVX, 0, WIDTH, self.view.frame.size.height - _titleView.frame.size.height);
        [_scrollView addSubview:childV];
        
    }
    
    HotViewController *hotvc = [[HotViewController alloc] init];
    hotvc.title = @"热文";
    
    MyViewController *mvc = [[MyViewController alloc] init];
    mvc.title = @"我的";
    
    LiZhiViewController *lzvc = [[LiZhiViewController alloc] init];
    //lzvc.title = @"励志";
    
    CarViewController *cvc = [[CarViewController alloc] init];
    //cvc.title = @"汽车";
    
    KeJiViewController *kjvc = [[KeJiViewController alloc] init];
    //kjvc.title = @"科技";
    
    ShiShangViewController *shvc = [[ShiShangViewController alloc] init];
    //shvc.title = @"时尚";
    
    CaiJingViewController *cjvc = [[CaiJingViewController alloc] init];
    //cjvc.title = @"财经";
    
    YuLeViewController *ylvc = [[YuLeViewController alloc] init];
    //ylvc.title = @"娱乐";
    NSArray *contorllerArr = @[lzvc,cvc,kjvc,shvc,cjvc,ylvc];
    NSMutableArray *contorllerArrS = [[NSMutableArray alloc]init];
    
    [contorllerArrS addObject:hotvc];
    [contorllerArrS addObject:mvc];

    for (int i = 0; i<_dataarr.count; i++) {
        if (contorllerArr.count > i) {
            UIViewController *viewController = contorllerArr[i];
            viewController.title = _dataarr[i];
            [contorllerArrS addObject:viewController];
        }
    }
    SCNavTabBarController *navTabBarController = [[SCNavTabBarController alloc] init];
    navTabBarController.subViewControllers = contorllerArrS;
    //navTabBarController.navTabBarColor = UIColorFromRGB(0xffffff);
    [navTabBarController addParentController:foodVc];
    
    [self addChildViewController:navTabBarController];
    
}


#pragma mark 滚动条
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    [self.view endEditing:YES];
    
    
    if(scrollView==_scrollView)
    {
        if(_scrollView.contentOffset.x / WIDTH ==0)
        {
            [_titleView wanerSelected:0];
        }
        else if (_scrollView.contentOffset.x / WIDTH ==1)
        {
            [_titleView wanerSelected:1];
        }
        
    }
}

#pragma mark titleView的方法
- (void)titleView:(DetailTitleView *)titleView scollToIndex:(NSInteger)tagIndex
{
    
    
    [_scrollView setContentOffset:CGPointMake(tagIndex * WIDTH, 0) animated:YES];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}

@end
